<?php
namespace App\Models\System;

use App\Exceptions\ApiException;
use App\Providers\LengthAwarePaginatorProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Enums\System\SysDictType\{Status};

/**
* 字典类型表 模型
* @create 2023-08-21 22:21:44
*/
class SysDictTypeModel extends \App\Models\BaseModel {
    use HasFactory;

    const Table         = "sys_dict_type";
    const PrimaryKey    = "dict_id";
    public $timestamps  = true;

    static $Wheres = ['dict_id', 'status'];     // 直接定义column, 默认where(column, '=', value)
    public function __construct($attributes = []) {
        parent::__construct($attributes);

        // static::$Wheres['dict_id']   = fn($q, $v) => $q->where(atn('dict_id'), '=', $v);
        static::$Wheres['dict_type']    = fn($q, $v) => $q->where(atn('dict_type'), 'like', "%{$v}%");
        static::$Wheres['dict_name']    = fn($q, $v) => $q->where(atn('dict_name'), 'like', "%{$v}%");
        static::$Wheres['begin_time']   = fn($q, $v) => $q->whereDate(atn('create_time'), '>=', $v);    // ymd
        static::$Wheres['end_time']     = fn($q, $v) => $q->whereDate(atn('create_time'), '<=', $v);    // ymd
    }

    /** xxxx */
    // public function user() {
        // return $this->hasOne(User::class, 'id', 'uid');
    // }

    /** ========== 常用查询 ========== */
    static $Vo  = ['dict_id', 'dict_name', 'dict_type', 'status', 'create_by', 'create_time', 'remark', ];
    static $Voi = ['*', ];

        /**
     * 根据条件分页查询字典类型
     * 
     * @param $dictType 字典类型信息
     * @return SysDictTypeModel[]|Collection 字典类型集合信息
     */
    public function selectDictTypeList(array $dictType): Collection {
        return $this->selectVo()
            ->wheres($dictType, 'dict_name', 'status', 'dict_type', 'begin_time', 'end_time')
            ->get()
            ;
    }

    /**
     * 根据所有字典类型
     * 
     * @return SysDictTypeModel[]|Collection 字典类型集合信息
     */
    public function selectDictTypeAll(): Collection {
        return $this->selectVo()
            ->get()
            ;
    }

    /**
     * 根据字典类型ID查询信息
     * 
     * @param $dictId 字典类型ID
     * @return SysDictTypeModel 字典类型
     */
    public function selectDictTypeById(int $dictId): SysDictTypeModel {
        return throw_str(
            $this->selectVo()
                ->where('dict_id', '=', $dictId)
                ->first() 
            ?? "字典类型($dictId)查找失败"
        );
    }

    /**
     * 根据字典类型查询信息
     * 
     * @param $dictType 字典类型
     * @return SysDictTypeModel 字典类型
     */
    public function selectDictTypeByType(string $dictType): SysDictTypeModel {
        return throw_str(
            $this->selectVo()
                ->where('dict_type', '=', $dictType)
                ->first() 
            ?? "字典类型($dictType)查找失败"
        );
    }

    /**
     * 通过字典ID删除字典信息
     * 
     * @param $dictId 字典ID
     * @return 结果
     */
    public function deleteDictTypeById(int $dictId): int {
        return $this->deleteDictTypeByIds([$dictId]);
    }

    /**
     * 批量删除字典类型信息
     * 
     * @param $dictIds 需要删除的字典ID
     * @return int 结果
     */
    public function deleteDictTypeByIds(array $dictIds): int {
        return $this
            ->whereIn('dict_id', $dictIds)
            ->delete()
            ;
    }

    /**
     * 新增字典类型信息
     * 
     * @param $dictType 字典类型信息
     * @return 结果
     */
    public function insertDictType(array $dictType): SysDictTypeModel {
        $values = static::when($dictType, 'dict_name', 'dict_type', 'status', 'remark', 'create_by');
        return throw_str(
            static::row_add($values)
        );
    }

    /**
     * 修改字典类型信息
     * 
     * @param $dictType 字典类型信息
     * @return int 结果
     */
    public function updateDictType(array $dictType): int {
        $values = static::when($dictType, 'dict_name', 'dict_type', 'status', 'remark', 'update_by');
        return static::query()
            ->where(atn('dict_id'), '=', get_value($dictType, 'dict_id'))
            ->update($values)
            ;
    }

    /**
     * 校验字典类型称是否唯一
     * 
     * @param $dictType 字典类型
     * @return SysDictTypeModel 结果
     */
    public function checkDictTypeUnique(string $dictType): SysDictTypeModel {
        return throw_str(
            $this->selectVo()
                ->where('dict_type', '=', $dictType)
                ->first()
            ?? "字典类型($dictType)查找失败"
        );
    }


    /** 根据DictId获取 */
    public function selectByDictId($dict_id): SysDictTypeModel {
        $where = [['dict_id', '=', "{$dict_id}"]];
        return throw_str(
            static::row($where) ?? "数据($dict_id)为空~"
        );
    }
    /** 根据DictType获取 */
    public function selectByDictType($dict_type): SysDictTypeModel {
        $where = [['dict_type', 'like', "%{$dict_type}%"]];
        return throw_str(
            static::row($where) ?? "数据($dict_type)为空~"
        );
    }


    /** 获取列表 @return SysDictType[]+paginator() */
    public function selectList(int|array $pag=1, array $param=[]): LengthAwarePaginatorProvider {
        return static::rows_pag($pag, [], fn($q)=>$q
                    // ->when(isset($param['dict_id']), fn($q)=>$q->where('dict_id', '=', $param['dict_id']))
                    ->wheres($param, 'dict_id','dict_type','status')
                    ->orderByDesc("dict_id")
        );
    }


    /** 插入数据 */
    public function insertData(array $data): SysDictTypeModel {
        return throw_str(
            static::row_add([
                // 'dict_id'        => $data['dict_id'],
                'dict_name'         => $data['dict_name'] ?? '',
                'dict_type'         => $data['dict_type'] ?? '',
                'status'            => $data['status'] ?? '0',
                'create_by'         => $data['create_by'] ?? '',
                // 'create_time'    => $data['create_time'] ?? '',
                'update_by'         => $data['update_by'] ?? '',
                // 'update_time'    => $data['update_time'] ?? '',
                'remark'            => $data['remark'] ?? '',
            ])
        );
    }


    /** 更新数据 */
    public function updateData(int|array $idOrWhere, array $data): SysDictTypeModel {
        $values = static::when(
                    $data, 
                    // 'dict_id',
                    'dict_name',
                    'dict_type',
                    'status',
                    'create_by',
                    // 'create_time',
                    'update_by',
                    // 'update_time',
                    'remark',
                );
        return throw_str(
            static::row_update($idOrWhere, $values)
        );
    }


    /** 删除数据 */
    public function deleteData(int|array $idOrWhere): SysDictTypeModel {
        return throw_str(
            static::row_delete($idOrWhere)
        );
    }



}